//
//  WmcEditTeamMemberInfoVC.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/14.
//

import UIKit

// MARK: - 扩展WmcEditTeamMemberInfoVC + WmcNavigatorProtocol

extension WmcEditTeamMemberInfoVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.editTeamMemberInfo]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vc = WmcEditTeamMemberInfoVC()
        if let vm = parms.context as? WmcEditTeamMemberInfoVM {
            vc = WmcEditTeamMemberInfoVC(vm)
        }
        return vc
    }
}

// MARK: - 扩展WmcEditTeamMemberInfoVC + WmcVMProtocol

extension WmcEditTeamMemberInfoVC: WmcVMProtocol {
    typealias VMType = WmcEditTeamMemberInfoVM
}

class WmcEditTeamMemberInfoVC: WmcBaseVC {
    override func viewDidLoad() {
        super.viewDidLoad()
        /// 获取详情信息
        viewModel.getMemberDetail()
        WmcNotificationManager.shared.addObserver(type: .editTeamRemarkSucceed) { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.getMemberDetail()
        }
    }

    override func makeUI() {
        title = "编辑资料"
        makeNavView(showShadow: false)

        view.addSubview(avatarView)
        view.addSubview(nameView)
        view.addSubview(noteView)
        view.addSubview(phoneView)
        view.addSubview(deleteView)

        avatarView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom)
            make.left.right.equalToSuperview()
        }

        nameView.snp.makeConstraints { make in
            make.top.equalTo(avatarView.snp.bottom)
            make.left.right.equalToSuperview()
        }

        noteView.snp.makeConstraints { make in
            make.top.equalTo(nameView.snp.bottom)
            make.left.right.equalToSuperview()
        }

        phoneView.snp.makeConstraints { make in
            make.top.equalTo(noteView.snp.bottom)
            make.left.right.equalToSuperview()
        }

        deleteView.snp.makeConstraints { make in
            make.top.equalTo(phoneView.snp.bottom)
            make.left.right.equalToSuperview()
        }
    }

    override func bindViewModel() {
        viewModel.teamMemberDetail.skip(1).bind { [weak self] detail in
            guard let weakSelf = self, let detail = detail else { return }
            weakSelf.avatarView.configure(model: detail)
            weakSelf.nameView.name = detail.name
            weakSelf.noteView.note = detail.remark
            weakSelf.phoneView.phone = detail.phone
        }.disposed(by: rx.disposeBag)

        if let isSelf = viewModel.teamMember?.isSelf() {
            if isSelf {
                deleteView.isHidden = false
            } else {
                WmcAuthManager.share.teamAblum = viewModel.teamAblum
                WmcAuthManager.share.getSelfMember { [weak self] selfMember in
                    guard let weakSelf = self else { return }
                    weakSelf.deleteView.isHidden = selfMember.role == .member
                }
            }
        }
    }

    override func bindViewAction() {
        noteView.rx.tapGesture().skip(1).bind { [weak self] _ in
            guard let weakSelf = self, var detail = weakSelf.viewModel.teamMemberDetail.value else { return }
            detail.teamId = weakSelf.viewModel.teamMember?.teamId
            WmcNavigatorManager.push(.editTeamMemerSpecificItem, parms: detail)
        }.disposed(by: rx.disposeBag)

        deleteView.deleteBtn.rx.controlEvent(.touchUpInside).bind { [weak self] _ in
            guard let weakSelf = self else { return }
            /// 删除成功之后，需要跳转,还需要弹窗
            /// 添加成功，返回刷新
            WmcCommonAlert.alert(msg: "确定要删除吗") {
                weakSelf.viewModel.deleteMember {
                    // 删除成功之后
                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
                        weakSelf.popToSpecificVC(vcClass: WmcTeamCloudAlbumMembersVC.self)
                        /// 通知刷新
                        WmcNotificationManager.shared.post(type: .deleteTeamMemberSucceed, object: nil)
                    }
                }
            }.show()
        }.disposed(by: rx.disposeBag)
    }

    lazy var avatarView: WmcEditTeamMemberInfoAvatarItemView = {
        let view = WmcEditTeamMemberInfoAvatarItemView()
        view.leftTitle = "头像"
        return view
    }()

    lazy var nameView: WmcEditTeamMemberInfoNameItemView = {
        let view = WmcEditTeamMemberInfoNameItemView()
        view.leftTitle = "姓名"
        return view
    }()

    lazy var noteView: WmcEditTeamMemberInfoNoteItemView = {
        let view = WmcEditTeamMemberInfoNoteItemView()
        view.leftTitle = "备注"
        return view
    }()

    lazy var phoneView: WmcEditTeamMemberInfoPhoneItemView = {
        let view = WmcEditTeamMemberInfoPhoneItemView()
        view.leftTitle = "手机号"
        return view
    }()

    lazy var deleteView: WmcEditTeamMemberInfoDeleteItemView = {
        let view = WmcEditTeamMemberInfoDeleteItemView()
        return view
    }()
}
